/// @Classname : HcCel
/// @Author :
/// @Time : 2023-05-29 9:34
/// @Description : //单元格

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/constants/hc_size.dart';
import 'package:hc_ui/theme/hc_cell_theme.dart';
import 'package:hc_ui/widget/cell_group/hc_cell_group.dart';
import 'package:hc_ui/widget/icon_text/hc_icon_text.dart';

class HcCell extends StatelessWidget {
  //padding
  final EdgeInsets padding;

  //标题文字
  final String? title;

  //标题组件
  final Widget? titleWidget;

  //副标题文字
  final String? subtitle;

  //副标题组件
  final Widget? subtitleWidget;

  //左侧组件
  final Widget? leading;

  //右侧组件
  final Widget? trailing;

  // 文字右侧 右侧图标之间组件
  final Widget? action;

  //是否展示箭头
  final bool showArrow;

  //是否禁用
  final bool disabled;

  //点击事件
  final VoidCallback? onTap;

  //长按事件
  final VoidCallback? onLongPress;

  final HcCellTheme? themeData;

  final bool isThreeLine;

  const HcCell(
      {super.key,
      this.padding = const EdgeInsets.all(HcSize.defaultPadding),
      this.title,
      this.titleWidget,
      this.subtitle,
      this.subtitleWidget,
      this.leading,
      this.trailing,
      this.showArrow = false,
      this.action,
      this.disabled = true,
      this.onTap,
      this.onLongPress,
      this.themeData,
      this.isThreeLine = false});

  @override
  Widget build(BuildContext context) {
    HcCellTheme theme = themeData ?? HcCellTheme();
    theme = HcCustomerConfig().allTheme.cellTheme.merge(theme);
    HcCellGroupScope? scope = HcCellGroup.of(context);
    //初始的圆角
    BorderRadius initialRadius = scope?.radius ?? theme.borderRadius;
    //当前的角标
    int currentIndex = scope?.children.indexOf(this) ?? 0;
    //总共展示行数
    int columns =
        scope != null ? (scope.children.length / scope.rowCounts).ceil() : 1;
    int rows = scope?.rowCounts ?? 1;
    //如果展示内部标题则第一行不修改圆角
    bool showTitle = scope?.showInsideTitle ?? false;
    //如果是第一个则圆角按照设定的来
    Radius topLeft =
        currentIndex == 0 && !showTitle ? initialRadius.topLeft : Radius.zero;
    Radius topRight =
        currentIndex % rows == rows - 1 && currentIndex < rows && !showTitle
            ? initialRadius.topRight
            : Radius.zero;
    Radius bottomLeft = currentIndex == (columns - 1) * rows
        ? initialRadius.bottomLeft
        : Radius.zero;
    Radius bottomRight = currentIndex == columns * rows - 1
        ? initialRadius.bottomRight
        : Radius.zero;
    bool finalShowArrow = scope?.showArrow ?? showArrow;
    HcCellType finalType = scope?.type ?? HcCellType.list;
    Widget finalTitleWidget = titleWidget ??
        Text(
          title ?? '',
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: theme.titleTextStyle,
        );
    Widget? finalSubtitleWidget = subtitleWidget ??
        (subtitle != null
            ? Text(
                subtitle!,
                maxLines: isThreeLine ? 2 : 1,
                overflow: TextOverflow.ellipsis,
                style: theme.subtitleTextStyle,
              )
            : null);
    Widget? trailingWidget = trailing ??
        (finalShowArrow ? const Icon(Icons.arrow_forward_ios_outlined) : null);

    return Ink(
      decoration: BoxDecoration(
          color: theme.backgroundColor,
          border: theme.border,
          borderRadius: BorderRadius.only(
            topLeft: topLeft,
            topRight: topRight,
            bottomLeft: bottomLeft,
            bottomRight: bottomRight,
          )),
      child: InkWell(
        borderRadius: BorderRadius.only(
          topLeft: topLeft,
          topRight: topRight,
          bottomLeft: bottomLeft,
          bottomRight: bottomRight,
        ),
        onTap: scope != null && scope.callback != null || onTap != null
            ? () {
                if (scope != null && scope.callback != null) {
                  scope.callback!("item", scope.children.indexOf(this), title);
                } else {
                  onTap!();
                }
              }
            : null,
        onLongPress: onLongPress != null ? () => onLongPress!() : null,
        child: finalType == HcCellType.grid
            ? Container(
                alignment: Alignment.center,
                padding: theme.cellGridPadding,
                child: HcIconText(
                  icon: leading != null
                      ? IconTheme(
                          data: IconThemeData(
                              size: theme.cellLeadingSize,
                              color: theme.leadingColor),
                          child: leading!,
                        )
                      : null,
                  direction: Axis.vertical,
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  spacing: theme.singleSubtitleSpacing,
                  label: Text(
                    title ?? "",
                    style: theme.titleTextStyle,
                  ),
                ),
              )
            : Container(
                padding: theme.cellListPadding,
                child: Row(
                  children: [
                    //构建左侧图标部分
                    if (leading != null)
                      Container(
                        padding: theme.leadingPadding,
                        margin: theme.leadingMargin,
                        child: IconTheme(
                          data: IconThemeData(
                              color: theme.leadingColor,
                              size: theme.leadingSize),
                          child: leading!,
                        ),
                      ),
                    Expanded(
                        child: Padding(
                      padding: theme.titlePadding.add(
                          finalSubtitleWidget != null
                              ? EdgeInsets.symmetric(
                                  vertical: -(isThreeLine
                                          ? theme.multipleSubtitleSpacing
                                          : theme.singleSubtitleSpacing) /
                                      2)
                              : EdgeInsets.zero),
                      child: Stack(
                        children: [
                          Row(
                            children: [
                              Expanded(
                                child: HcIconText(
                                  direction: Axis.vertical,
                                  spacing: isThreeLine
                                      ? theme.multipleSubtitleSpacing
                                      : theme.singleSubtitleSpacing,
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  icon: finalTitleWidget,
                                  label: finalSubtitleWidget,
                                ),
                              ),
                              if (action != null)
                                Expanded(
                                    child: Container(
                                      margin: theme.actionMargin,
                                  alignment: Alignment.centerRight,
                                  child: DefaultTextStyle(
                                    style: theme.subtitleTextStyle,
                                    child: action!,
                                  ),
                                ))
                            ],
                          )
                        ],
                      ),
                    )),

                    //构建右侧图标部分
                    if (trailingWidget != null)
                      Container(
                        margin: theme.trailingMargin,
                        child: IconTheme(
                          data: IconThemeData(
                              color: theme.trailingColor,
                              size: theme.trailingSize),
                          child: trailingWidget,
                        ),
                      ),
                  ],
                ),
              ),
      ),
    );
  }
}
